package com.qidian.buy;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;

import java.sql.Types;
import java.util.Collections;

/**
 * Author: 戴着假发的程序员
 * Date: 2024-04-29 10:29
 * company: 起点编程
 **/

public class MyBatisPlusGen {

    @Test
    public void testIntAndInteger(){
        int x = 10;
        int y = 10;
        System.out.println(x == y); // true
        Integer a = new Integer(10);
        Integer b = new Integer(10);
        System.out.println(a == b); // false

        Integer c = 100;   // -128 ~ 127
        Integer d = 100;
        System.out.println( c == d );//
    }

    @Test
    public void testGen(){
        String url = "jdbc:mysql://localhost:3306/qd-buy";
        FastAutoGenerator.create(url, "root", "root123")
                .globalConfig(builder -> {
                    builder.author("戴着假发的程序员") // 设置作者
                            //.enableSwagger() // 开启 swagger 模式
                            //.fileOverride() // 覆盖已生成文件
                            .outputDir("D:\\cx642\\课堂\\18.分布式项目\\temp"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.qidian.buy") // 设置父包名
                            .moduleName("order") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\cx642\\课堂\\18.分布式项目\\temp\\mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("buy_logistics","buy_orderitem","buy_order") // 设置需要生成的表名
                            .addTablePrefix("buy_", "md_","qc_","pro_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
